﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
<link href="~/lib/layui/css/layui.css" rel="stylesheet" />
<script src="~/lib/layui/layui.js"></script>
<script src="~/lib/jquery/dist/jquery.js"></script>
<h3 style="text-align:center">登录页面</h3>

<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Demo</title>

    <!-- 请勿在项目正式环境中引用该 layui.css 地址 -->
    <link href="//unpkg.com/layui@2.9.18/dist/css/layui.css" rel="stylesheet">
</head>
<body>
    <style>
        .demo-login-container {
            width: 320px;
            margin: 21px auto 0;
        }

        .demo-login-other .layui-icon {
            position: relative;
            display: inline-block;
            margin: 0 2px;
            top: 2px;
            font-size: 26px;
        }
    </style>
    <form class="layui-form">
        <div class="demo-login-container">
            <div class="layui-form-item">
                <div class="layui-input-wrap">
                    <div class="layui-input-prefix">
                        <i class="layui-icon layui-icon-username"></i>
                    </div>
                    <input type="text" name="userName" value="" id="userName" lay-verify="required" placeholder="用户名" lay-reqtext="请填写用户名" autocomplete="off" class="layui-input" lay-affix="clear">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-wrap">
                    <div class="layui-input-prefix">
                        <i class="layui-icon layui-icon-password"></i>
                    </div>
                    <input type="password" name="userPwd" value="" lay-verify="required" id="userPwd" placeholder="密   码" lay-reqtext="请填写密码" autocomplete="off" class="layui-input" lay-affix="eye">
                </div>
            </div>


            <div class="layui-form-item">
                <a href="#" id="useEmail">邮箱登录</a>  <button type="button" class="layui-btn layui-btn-fluid" lay-submit lay-filter="demo-login" lay-on="msg" id="loginBtn">登录</button>
            </div>


        </div>
    </form>
    <script>
        document.getElementById('useEmail').addEventListener('click', function (e) {
            e.preventDefault(); // 阻止链接默认行为
            window.location.href = '/Users/EmailView'; // 重定向到邮箱验证页面
        });
    </script>

    <!-- 请勿在项目正式环境中引用该 layui.js 地址 -->
    <script src="//unpkg.com/layui@2.9.18/dist/layui.js"></script>
    <script>
        layui.use(function () {
            var form = layui.form;
            var layer = layui.layer;

        });
    </script>

</body>
</html>
@* <script src="https://cdnjs.cloudflare.com/ajax/libs/js - jwt/3.0.1/index.js"></script> *@



<script>
    layui.use(function () {
        var form = layui.form;
        var layer = layui.layer;

    });
    layui.use(['jquery', 'layer'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;

        // 登录按钮点击事件
        $('#loginBtn').on('click', function () {
            var userName = $('#userName').val();
            var userPwd = $('#userPwd').val();

            if (userName == '' || userPwd == '') {
                layer.msg('用户名或密码不能为空！');
                return false;
            }
            $.ajax({
                url: '@ViewBag.Darius' + "/api/Users/Login",
                type: "post",
                async: false,
                data: JSON.stringify({ userAccount: userName, userPwd: userPwd }),
                dataType: "json",
                contentType: "application/json",
                success: function (res) {
                    console.log(res)
                    if (res.list.data != null) {
                        localStorage.setItem('jwt', res.token);
                        localStorage.setItem('username', userName);
                        layer.msg(res.list.messege);
                        location.href = "/Users/MenuView" 
                    } else {
                        layer.msg(res.msg);
                    }
                }
            })
           
        });
    });

    //JWT验证
    function fetchProtectedData(){
        const token = localStorage.getItem("jwt");
        if (token) {
            fetch('@ViewBag.Darius' + '/api/Users/Login', {
                headers: {
                    'Authorization': 'Bearer'+ token
                }
            })
             
        }
    }
        

</script>

